package com.sophialex.health.record.common.showKtable;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.sophialex.health.record.common.Messages;
import com.sophialex.health.system.frame.resource.RcpResourceManager;
import com.sophialex.health.system.frame.util.LayoutUtil;

import de.kupzog.ktable.KTable;

public class ShowKTableStyleDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private TabFolder tabFolder;
	private Button button_1;
	private Table table;
	private Label label;
	private KTable ktable;
	private Font font = RcpResourceManager.getFont("宋体", 11, SWT.NORMAL); //$NON-NLS-1$
	private int selected = 0;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 * @param ktable
	 * @param selected
	 *            选项卡打开项 0 表格属性 1表格显项
	 */
	public ShowKTableStyleDialog(Shell parent, int style, KTable ktable,
			int selected) {
		super(parent, style);
		setText(Messages.ShowKTableStyleDialog_1);
		this.ktable = ktable;
		this.selected = selected;
	}

	public ShowKTableStyleDialog(Shell parent, int style, KTable ktable) {
		super(parent, style);
		setText(Messages.ShowKTableStyleDialog_2);
		this.ktable = ktable;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		initTable();
		tabFolder.setSelection(selected);
		Display display = getParent().getDisplay();
		LayoutUtil.centerShell(display, shell);
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(450, 388);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		{
			tabFolder = new TabFolder(shell, SWT.NONE);
			{
				FormData formData = new FormData();
				formData.top = new FormAttachment(0, 5);
				formData.left = new FormAttachment(0);
				formData.bottom = new FormAttachment(100, -35);
				formData.right = new FormAttachment(100);
				tabFolder.setLayoutData(formData);
			}
			{
				TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
				tabItem.setText(Messages.ShowKTableStyleDialog_3);
			}
			{
				TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
				tabItem.setText(Messages.ShowKTableStyleDialog_4);
				{
					Composite composite = new Composite(tabFolder, SWT.NONE);
					tabItem.setControl(composite);
					composite.setLayout(new FormLayout());
					{
						label = new Label(composite, SWT.NONE);
						label.setFont(RcpResourceManager.getFont("宋体", 10, //$NON-NLS-1$
								SWT.NORMAL));
						{
							FormData formData = new FormData();
							formData.top = new FormAttachment(0, 10);
							formData.left = new FormAttachment(0, 10);
							label.setLayoutData(formData);
						}
						label.setText(Messages.ShowKTableStyleDialog_6);
					}
					{
						table = new Table(composite, SWT.BORDER | SWT.CHECK
								| SWT.FULL_SELECTION);
						table.setFont(RcpResourceManager.getFont("宋体", 9, //$NON-NLS-1$
								SWT.NORMAL));
						{
							FormData formData = new FormData();
							formData.bottom = new FormAttachment(100, 0);
							formData.right = new FormAttachment(100, -10);
							formData.top = new FormAttachment(label, 6);
							formData.left = new FormAttachment(label, 0,
									SWT.LEFT);
							table.setLayoutData(formData);
						}
						table.setHeaderVisible(true);
						{
							TableColumn tableColumn = new TableColumn(table,
									SWT.NONE);
							tableColumn.setWidth(72);
							tableColumn.setText(Messages.ShowKTableStyleDialog_8);
						}
						{
							TableColumn tableColumn = new TableColumn(table,
									SWT.NONE);
							tableColumn.setWidth(332);
							tableColumn.setText(Messages.ShowKTableStyleDialog_9);
						}
					}
				}
			}
		}
		{
			button_1 = new Button(shell, SWT.NONE);
			{
				FormData formData = new FormData();
				formData.top = new FormAttachment(tabFolder, 6);
				formData.right = new FormAttachment(100, -10);
				button_1.setLayoutData(formData);
			}
			button_1.setText(Messages.ShowKTableStyleDialog_10);
			button_1.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					doExit();
				}
			});
		}
		{
			Button button = new Button(shell, SWT.NONE);
			{
				FormData formData = new FormData();
				formData.top = new FormAttachment(tabFolder, 6);
				formData.right = new FormAttachment(button_1, -6);
				button.setLayoutData(formData);
			}
			button.setText(Messages.ShowKTableStyleDialog_11);
			button.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					doSave();
				}
			});
		}
	}

	protected void doExit() {
		shell.close();
	}

	private void initTable() {
		int count = ktable.getModel().getColumnCount();
		String[] columns = new String[count];
		int[] widths = new int[count];
		TableItem item;
		for (int i = 0; i < count; i++) {
			widths[i] = ktable.getModel().getColumnWidth(i);
			columns[i] = ktable.getModel().getContentAt(i, 0).toString();
			item = new TableItem(table, SWT.NONE);
			item.setText(new String[] { "", //$NON-NLS-1$
					i == 0 && "".equals(columns[i]) ? Messages.ShowKTableStyleDialog_14 : columns[i] }); //$NON-NLS-1$
			item.setChecked(widths[i] > 0);
			item.setData(widths[i]);
			item.setFont(font);
		}
	}

	public void doSave() {
		int i = 0;
		Integer formerW;
		for (TableItem item : table.getItems()) {
			if (!item.getChecked()) {
				ktable.getModel().setColumnWidth(i, 0);
			} else {
				formerW = (Integer) item.getData();
				if (formerW <= 0) {
					ktable.getModel().setColumnWidth(i,
							i == 0 && item.getText(1).equals(Messages.ShowKTableStyleDialog_15) ? 30 : 80);
				}
			}
			i++;
		}
		ktable.redraw();
		shell.close();
	}
}
